package apis

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"fmt"
	"os"
	"io"
	"hulk-api/models"
	"strings"
)

func Upload(c *gin.Context)  {
	file,header,err := c.Request.FormFile("file")
	if err != nil {
		c.String(http.StatusBadRequest,fmt.Sprintf("get file err : %s", err.Error()))
		return
	}
	filename := header.Filename
	fmt.Println("filename is ...." , filename)

	tmp := models.TemPath{
		Name: strings.Split(filename,".")[0],
		Path: "/template/yaml/ansible/" + filename,
	}
	err = tmp.AddTemplate()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
		return
	}
	out, err := os.Create("template/yaml/ansible/" + filename)
	if err != nil {
		//log.Fatal(err)
		fmt.Println(err)
	}
	defer out.Close()
	_, err = io.Copy(out, file)
	if err != nil {
		fmt.Println(err)
	}
	c.JSON(http.StatusOK, gin.H{"filename": filename})
}

//func UploadReport(c *gin.Context)  {
//
//	file,header,err := c.Request.FormFile("file")
//	if err != nil {
//		c.String(http.StatusBadRequest,fmt.Sprintf("get file err : %s", err.Error()))
//		return
//	}
//	//fmt.Println(file,header)
//	//fmt.Println("fuckdvd")
//	//fmt.Println(c.Request.Form)
//
//	workflow := cicd.DeploymentFlow{
//		//Title: c.Request.Form['']
//		Title: c.Request.Form.Get("title"),
//		Content: c.Request.Form.Get("content"),
//		OnlineTime: c.Request.Form.Get("onlinetime"),
//		ItemList: c.Request.Form.Get("itemlist"),
//		DbList: c.Request.Form.Get("dblist"),
//		SqlList: c.Request.Form.Get("sqllist"),
//
//		TestReportFilename: header.Filename,
//		TestReportContent: file,
//
//	}
//	workflow.Create()
//	c.JSON(http.StatusOK, gin.H{"filename": "ojbk"})
//
//}

func GetFileStat(c *gin.Context)  {
	fpath:= c.Query("filepath")
	fileInfo, err := os.Stat(fpath)
	if err != nil {
		fmt.Println("get file size erris " , err.Error())
	}
	fileSize := fileInfo.Size() //获取size BYTE
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"size":fileSize,
	})

}

